package com.guoheng.hazard.sys.service.impl.hazard;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.hazard.common.base.Result;
import com.guoheng.hazard.sys.mapper.entpr.EntprBasicInfoMapper;
import com.guoheng.hazard.sys.mapper.hazard.KeyRegulatoryChemicalMapper;
import com.guoheng.hazard.sys.model.dto.hazard.KeyRegulatoryChemicalInsertDTO;
import com.guoheng.hazard.sys.model.dto.hazard.KeyRegulatoryChemicalSelectDTO;
import com.guoheng.hazard.sys.model.dto.hazard.KeyRegulatoryChemicalUpdateDTO;
import com.guoheng.hazard.sys.model.entity.entpr.EntprBasicInfo;
import com.guoheng.hazard.sys.model.entity.hazard.KeyRegulatoryChemical;
import com.guoheng.hazard.sys.service.hazard.KeyRegulatoryChemicalService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
 * 类功能描述:　KeyRegulatoryChemicalServiceImpl
 *
 * @author fbl
 * @date 2019-05-13 8:05
 */
@Service
public class KeyRegulatoryChemicalServiceImpl implements KeyRegulatoryChemicalService {

    @Resource
    private KeyRegulatoryChemicalMapper keyRegulatoryChemicalMapper;

    @Resource
    EntprBasicInfoMapper entprBasicInfoMapper;

    @Override
    public Result getChemicals(String entprName,String productName, Long entprId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<KeyRegulatoryChemicalSelectDTO> keyRegulatoryChemicalSelectDTOS = new ArrayList<>();
        List<KeyRegulatoryChemical> chemicals = keyRegulatoryChemicalMapper.getChemicals(entprName,productName,entprId);

        // 得到企业姓名
        for (KeyRegulatoryChemical chemical : chemicals) {
            KeyRegulatoryChemicalSelectDTO keyRegulatoryChemicalSelectDTO = new KeyRegulatoryChemicalSelectDTO();
            BeanUtils.copyProperties(chemical,keyRegulatoryChemicalSelectDTO);
            Long eId = chemical.getEntprId();
            EntprBasicInfo entprBasicInfoById = entprBasicInfoMapper.getEntprBasicInfoById(eId);
            keyRegulatoryChemicalSelectDTO.setEntprName(entprBasicInfoById.getEntprName());
            keyRegulatoryChemicalSelectDTOS.add(keyRegulatoryChemicalSelectDTO);
        }
        PageInfo<KeyRegulatoryChemicalSelectDTO> keyRegulatoryChemicalPageInfo = new PageInfo<>(keyRegulatoryChemicalSelectDTOS);
        return Result.success(keyRegulatoryChemicalPageInfo);
    }

    @Override
    public Result updateChemicals(KeyRegulatoryChemicalUpdateDTO keyRegulatoryChemicalUpdateDTO) {
        KeyRegulatoryChemical keyRegulatoryChemical = new KeyRegulatoryChemical();
        BeanUtils.copyProperties(keyRegulatoryChemicalUpdateDTO,keyRegulatoryChemical);
        keyRegulatoryChemicalMapper.updateChemicals(keyRegulatoryChemical);
        return Result.success();
    }

    @Override
    public Result insertChemicals(KeyRegulatoryChemicalInsertDTO keyRegulatoryChemicalInsertDTO) {
        KeyRegulatoryChemical keyRegulatoryChemical = new KeyRegulatoryChemical();
        BeanUtils.copyProperties(keyRegulatoryChemicalInsertDTO,keyRegulatoryChemical);
        keyRegulatoryChemicalMapper.insertChemicals(keyRegulatoryChemical);
        return Result.success();
    }

    @Override
    public Result deleteChemicals(Long id) {
        KeyRegulatoryChemical keyRegulatoryChemical = new KeyRegulatoryChemical();
        keyRegulatoryChemical.setId(id);
        keyRegulatoryChemical.setDelFlag(true);
        keyRegulatoryChemicalMapper.updateChemicals(keyRegulatoryChemical);
        return Result.success();
    }

    @Override
    public Result getChemicalById(Long id) {
        KeyRegulatoryChemical chemicalById = keyRegulatoryChemicalMapper.getChemicalById(id);
        return Result.success(chemicalById);
    }
}
